package com.urms.service.serviceImpl;

import com.urms.mapper.UserMapper;
import com.urms.pojo.Result;
import com.urms.pojo.User;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
@Component
public class AdminRole implements UserRole {

    @Resource
    private UserMapper userMapper;

    public static AdminRole adminRoleMapper;

    @PostConstruct
    public void init(){
        adminRoleMapper = this ;
        adminRoleMapper.userMapper = userMapper;
    }
    @Override
    public Result register(User user) {
        if(user.getPhone()==null){
            return Result.error("手机号不能为空");
        }
        if(adminRoleMapper.userMapper.queryAdminByPhone(user.getPhone())!=null){
            return Result.error("该手机号已被注册");
        }
        adminRoleMapper.userMapper.insertAdmin(user);
        return Result.success("注册成功");
    }

    @Override
    public Result login(User user) {
        User admin = adminRoleMapper.userMapper.queryStudentByPhone(user.getPhone());
        if(admin==null){
            return Result.error("该用户不存在");
        }
        if(!admin.getPassword().equals(user.getPassword())){
            return Result.error("密码错误");
        }
        return Result.success("登录成功", admin);
    }
}
